﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace SimpleSM
{
    class Group : DB
    {
        public DataTable Get()
        {
            return this.Select("SELECT * FROM [group] ORDER BY name ASC;");
        }

        public DataTable Get(int id)
        {
            return this.Select("SELECT * FROM [group] WHERE id = @id;", new string[1, 2] { { "@id", id.ToString() } });
        }

        public bool Add(string name, int group_id = 0)
        {
            if (group_id == 0)
            {
                return this.NonSelect("INSERT INTO [group] (name) VALUES (@name);", new string[1, 2] { { "@name", name } });
            }
            else
            {
                return this.NonSelect("INSERT INTO [group] (name, group_id) VALUES (@name, @group_id);", new string[2, 2] { { "@name", name }, { "@group_id", group_id.ToString() } });
            }
        }

        public bool Update(string name, int id, int group_id)
        {
            if (group_id == 0)
            {
                return this.NonSelect("UPDATE [group] SET name = @name, group_id = NULL WHERE id = @id;", new string[2, 2] { { "@name", name }, { "@id", id.ToString() } });
            }
            else
            {
                return this.NonSelect("UPDATE [group] SET name = @name, group_id = @group_id WHERE id = @id;", new string[3, 2] { { "@name", name }, { "@id", id.ToString() }, { "@group_id", group_id.ToString() } });
            }
        }

        public bool Update(string name, int id)
        {
            return this.NonSelect("UPDATE [group] SET name = @name WHERE id = @id;", new string[2, 2] { { "@name", name }, { "@id", id.ToString() } });
        }

        public bool Update(bool expand, int id)
        {
            return this.NonSelect("UPDATE [group] SET expand = @expand WHERE id = @id;", new string[2, 2] { { "@id", id.ToString() }, { "@expand", ((expand) ? 1 : 0).ToString() } });
        }

        public bool Delete(int id)
        {
            return this.NonSelect("DELETE FROM [group] WHERE id = @id;", new string[1, 2] { { "@id", id.ToString() } });
        }
    }
}
